#include "SettingState.h"
//SGD Wrappers
#include "../../SGD_Wrappers/CSGD_Direct3D.h"
#include "../../SGD_Wrappers/CSGD_DirectInput.h"
#include "../../SGD_Wrappers/CSGD_TextureManager.h"
#include "../../SGD_Wrappers/CSGD_XAudio2.h"
#include "../../SGD_Wrappers/SGD_String.h"
#include "../../SGD_Wrappers/CSGD_EventSystem.h"
#include "../../SGD_Wrappers/CEvent.h"
#include "../Game.h"
#include "../States/MainMenuState.h"
#include "../../SGD_Wrappers/SGD_Math.h"
#include <fstream>
#include <iostream>
using namespace std;
#include "../../SGD_Wrappers/SGD_String.h"
#include "../../SGD_Wrappers/SGD_Util.h"
#include "../SoundSystem/SoundSystem.h"
#include "../DrawText.h"


CSettingState::CSettingState()
{
	m_rSliderBox.left = 300;
	m_rSliderBox.top = 100;
	m_rSliderBox.right = 400;
	m_rSliderBox.bottom = 125;
	m_rSlider.left = (LONG)m_pXA->GetInstance()->SFXGetMasterVolume()*400-5;
	m_rSlider.right = m_rSlider.left+15;
	m_rSlider.top = 100;
	m_rSlider.bottom = 125;
	m_nCursorPos = 1;
	m_nTextImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("text_0.png"));
}

CSettingState::~CSettingState()
{

}

CSettingState*	CSettingState::GetInstance( void )
{
	//Store an instance in global memory
	static CSettingState s_Instance;

	//Return the instance
	return &s_Instance;
}

// IGameState Interface
void	CSettingState::Enter	( void )
{
	// Store the SGD Wrapper singletons
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();

	ifstream fin;

	fin.open("Settings.txt",ios_base::in);

	if(fin.good())
		{
			float fSound;
			fin >> fSound;
			SoundSystem::GetInstance()->SetVolume(fSound);
		}

	fin.close();
	

}

void	CSettingState::Exit	( void )
{
	
}

bool	CSettingState::Input	( void )
{
	if( m_pDI->KeyPressed( DIK_RETURN ) )
	{
		ofstream fout("Settings.txt", ios_base::out);
		if(fout.is_open())
		{
			if(fout.good())
			{
				fout << SoundSystem::GetInstance()->GetVolume() << "\n" << CGame::GetInstance()->GetWindow();
			}
			fout.close();
		}
		CGame::GetInstance()->PopState();
	}
	POINT mouse;
	SetCursorPos(0,0);
	GetCursorPos(&mouse);
	RECT mouseRect = {mouse.x,mouse.y,mouse.x+100,mouse.y+100};
	if(mouse.x >= m_rSlider.left && mouse.x <= m_rSlider.right 
		&& mouse.y >= m_rSlider.top && mouse.y <= m_rSlider.bottom)
	{
		if(m_pDI->MouseButtonDown(0))
		{
			m_rSlider.left = mouse.x-5;
			m_rSlider.right = mouse.x+10;
		}
	}

	if(m_pDI->KeyPressed(DIK_DOWNARROW))
		m_nCursorPos++;
	if(m_pDI->KeyPressed(DIK_UPARROW))
		m_nCursorPos--;

	if(m_nCursorPos < 1)
		m_nCursorPos = 2;
	if(m_nCursorPos > 2)
		m_nCursorPos = 1;
	

	return true;
}

void	CSettingState::Update	( float fElapsedTime )
{
	if(m_nCursorPos == 1)
	{
		if(m_pDI->KeyDown(DIK_RIGHTARROW))
		{
			SoundSystem::GetInstance()->SetVolume(SoundSystem::GetInstance()->GetVolume()+ 0.1f * fElapsedTime);
		}
		else if(m_pDI->KeyDown(DIK_LEFTARROW))
		{
			SoundSystem::GetInstance()->SetVolume(SoundSystem::GetInstance()->GetVolume()- 0.1f * fElapsedTime);
		}
	}

	if(m_nCursorPos == 2)
	{
		if(m_pDI->KeyDown(DIK_RIGHTARROW))
		{
			CGame::GetInstance()->SetWindow(!CGame::GetInstance()->GetWindow());
		}
		else if(m_pDI->KeyDown(DIK_LEFTARROW))
		{
			CGame::GetInstance()->SetWindow(!CGame::GetInstance()->GetWindow());
		}
	}

	if(SoundSystem::GetInstance()->GetVolume() > 1.0f)
		SoundSystem::GetInstance()->SetVolume(1.0f);
	if(SoundSystem::GetInstance()->GetVolume() < 0.0f)
		SoundSystem::GetInstance()->SetVolume(0.0f);
}

void	CSettingState::Render	( void )
{
	m_pD3D->DeviceBegin();
	m_pD3D->Clear( 255, 0, 0 );
	std::ostringstream ToScreen;
	float t = SoundSystem::GetInstance()->GetVolume()*100;
	int temp = (int)t;
	ToScreen << temp;
	RECT Cursor = {0,0,0,0};
	if(m_nCursorPos == 1)
	{
		Cursor.left = 50;
		Cursor.top = 100;
		Cursor.bottom = 130;
		Cursor.right = 90;
		m_pD3D->DrawRect(Cursor,0,255,255);
	}

	if(m_nCursorPos == 2)
	{
		Cursor.left = 50;
		Cursor.top = 180;
		Cursor.bottom = 210;
		Cursor.right = 90;
		m_pD3D->DrawRect(Cursor,0,255,255);
	}


	CDrawText::GetInstance()->Print(m_nTextImageID,"Volume: ",100,100,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
	CDrawText::GetInstance()->Print(m_nTextImageID,ToScreen.str().c_str(),380, 100,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
	ToScreen.str("");
	CDrawText::GetInstance()->Print(m_nTextImageID,"Windowed: ",100,180,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
	if(CGame::GetInstance()->GetWindow())
		CDrawText::GetInstance()->Print(m_nTextImageID,"Yes",330,180,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
	else 
		CDrawText::GetInstance()->Print(m_nTextImageID,"No",330,180,1.0f,D3DCOLOR_ARGB(255, 140, 0,0));
		

	m_pD3D->DeviceEnd();
}


void CSettingState::HandleEvent(CEvent* pEvent)
{

}
